using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using Forecast.Business;

public partial class controls_forms_PredictionForm : System.Web.UI.UserControl
{
    public SaveComplete OnSaveComplete = null;

    public int PredictionId
    {
        get
        {
            if (ViewState["PredictionId"] != null)
                return Convert.ToInt32(ViewState["PredictionId"]);
            else
                return 0;
        }
        set
        {
            ViewState["PredictionId"] = value;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ToggleCreateMode(true);
        }
    }

    private void ResetForm()
    {
        ToggleCreateMode(true);
        _ticker.Text = "";
        _shortDescription.Text = "";
        _longDescription.Text = "";
        _endDate.Text = "";
        PredictionId = 0;
    }

    private void ToggleCreateMode(bool newPrediction)
    {
        _create.Visible = newPrediction;
        _activate.Visible = !newPrediction;
        _endFailed.Visible = !newPrediction;
        _endSuccess.Visible = !newPrediction;
        _update.Visible = !newPrediction;
        _delete.Visible = !newPrediction;
        _cancel.Visible = !newPrediction;
    }

    public void LoadPrediction(Int32 predictionId, bool possibleToDelete)
    {
        string sql = "SELECT * FROM [Prediction] WHERE ID = ?";
        OleDbConnection cn = new OleDbConnection(ConfigurationManager.ConnectionStrings["ForecastDB"].ConnectionString);
        cn.Open();
        OleDbCommand cm = new OleDbCommand(sql, cn);
        cm.Parameters.Add("ID", OleDbType.Integer).Value = predictionId;
        try
        {
            OleDbDataReader reader = cm.ExecuteReader();

            while (reader.Read())
            {
                _ticker.Text = reader["Ticker"].ToString();
                _shortDescription.Text = reader["Short_description"].ToString();
                _longDescription.Text = reader["Long_description"].ToString();
                _endDate.Text = reader["End_date"].ToString();                
            }
            reader.Close();
        }
        finally
        {
            cn.Close();
        }

        PredictionId = predictionId;

        ToggleCreateMode(false);
        _delete.Visible = possibleToDelete;
    }

    protected void _create_Click(object sender, EventArgs e)
    {
        string sql = "INSERT INTO [Prediction] ([Ticker], [Short_description], [Long_description], [End_date], [Last_value]) VALUES (?,?,?,?,?)";

        OleDbConnection cn = new OleDbConnection(ConfigurationManager.ConnectionStrings["ForecastDB"].ConnectionString);
        cn.Open();
        OleDbCommand cm = new OleDbCommand(sql, cn);
        cm.Parameters.Add("Ticker", OleDbType.VarWChar).Value = _ticker.Text;
        cm.Parameters.Add("Short_description", OleDbType.VarWChar).Value = _shortDescription.Text;
        cm.Parameters.Add("Long_description", OleDbType.VarWChar).Value = _longDescription.Text;
        cm.Parameters.Add("End_date", OleDbType.Date).Value = GetEndDateFromUI(); //TODO
        cm.Parameters.Add("Last_value", OleDbType.Integer).Value = 50;
        try
        {
            int result = cm.ExecuteNonQuery();
        }
        finally
        {
            cn.Close();
        }

        ResetForm();

        if (OnSaveComplete != null)
            OnSaveComplete();
    }

    private DateTime GetEndDateFromUI()
    {
        return Convert.ToDateTime(_endDate.Text);
    }


    protected void _activate_Click(object sender, EventArgs e)
    {
        Activity activity = new Activity(ConfigurationManager.ConnectionStrings["ForecastDB"].ConnectionString);
        activity.ActivatePrediction(PredictionId);

        ResetForm();

        if (OnSaveComplete != null)
            OnSaveComplete();
    }

    protected void _endFailed_Click(object sender, EventArgs e)
    {
        Activity activity = new Activity(ConfigurationManager.ConnectionStrings["ForecastDB"].ConnectionString);
        activity.PredictionEnd(PredictionId, false);
        ResetForm();

        if (OnSaveComplete != null)
            OnSaveComplete();
    }

    protected void _endSuccess_Click(object sender, EventArgs e)
    {
        Activity activity = new Activity(ConfigurationManager.ConnectionStrings["ForecastDB"].ConnectionString);
        activity.PredictionEnd(PredictionId, true);
        ResetForm();

        if (OnSaveComplete != null)
            OnSaveComplete();
    }

    protected void _update_Click(object sender, EventArgs e)
    {
        string sql = "UPDATE [Prediction] SET [Ticker] = ?, [Short_description] = ?, [Long_description] = ?, [End_date] = ? WHERE ID = ?";

        OleDbConnection cn = new OleDbConnection(ConfigurationManager.ConnectionStrings["ForecastDB"].ConnectionString);
        cn.Open();
        OleDbCommand cm = new OleDbCommand(sql, cn);
        cm.Parameters.Add("Ticker", OleDbType.VarWChar).Value = _ticker.Text;
        cm.Parameters.Add("Short_description", OleDbType.VarWChar).Value = _shortDescription.Text;
        cm.Parameters.Add("Long_description", OleDbType.VarWChar).Value = _longDescription.Text;
        cm.Parameters.Add("End_date", OleDbType.Date).Value = GetEndDateFromUI();
        cm.Parameters.Add("ID", OleDbType.Integer).Value = PredictionId;
        try
        {
            int result = cm.ExecuteNonQuery();
        }
        finally
        {
            cn.Close();
        }

        ResetForm();

        if (OnSaveComplete != null)
            OnSaveComplete();
    }

    protected void _delete_Click(object sender, EventArgs e)
    {
        string sql = "DELETE FROM [Prediction] WHERE ID = ?";

        OleDbConnection cn = new OleDbConnection(ConfigurationManager.ConnectionStrings["ForecastDB"].ConnectionString);
        cn.Open();
        OleDbCommand cm = new OleDbCommand(sql, cn);
        cm.Parameters.Add("ID", OleDbType.Integer).Value = PredictionId;
        try
        {
            int result = cm.ExecuteNonQuery();
        }
        finally
        {
            cn.Close();
        }

        ResetForm();

        if (OnSaveComplete != null)
            OnSaveComplete();
    }

    protected void _cancel_Click(object sender, EventArgs e)
    {
        ResetForm();
    }
}
